//
//  NSDate+KY.m
//  KuaiyanCalendar
//
//  Created by 罗新 on 2017/10/17.
//  Copyright © 2017年 KuaiYan  Co.,LTD. All rights reserved.
//

#import "NSDate+KY.h"

@implementation NSDate (KY)

/**
 获取当月天数
 */
+ (NSInteger)numberOfDaysInCurrentMonth {
    return [self totaldaysInMonth:[NSDate date]];
}


/**
 获取当月的周数
 */
+ (NSInteger)numberOfWeeksInCurrentMonth {
    // 初始化日历
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    // 获取系统当前日期
    NSDate *currentDate = [NSDate date];
    // 获取当前日期中当前月中周的范围
    NSRange range = [calendar rangeOfUnit:NSCalendarUnitWeekOfMonth inUnit:NSCalendarUnitMonth forDate:currentDate];
    // 得到当前月中总共有多少周（即范围的长度）
    NSInteger numberOfWeeksInCurrentMonth = range.length;
    return numberOfWeeksInCurrentMonth;
}

/**
 获取当月1号所在星期
 */
+ (NSInteger)firstWeekdayInCurrentMonth{
    return [self firstWeekdayInThisMonth:[NSDate date]];
}

+ (NSInteger)day:(NSDate *)date{
    NSDateComponents *components = [[NSCalendar currentCalendar] components:(NSCalendarUnitDay) fromDate:date];
    return [components day];
}

+ (NSInteger)month:(NSDate *)date{
    NSDateComponents *components = [[NSCalendar currentCalendar] components:(NSCalendarUnitMonth) fromDate:date];
    return [components month];
}

+ (NSInteger)year:(NSDate *)date{
    NSDateComponents *components = [[NSCalendar currentCalendar] components:(NSCalendarUnitYear) fromDate:date];
    return [components year];
}


+ (NSInteger)firstWeekdayInThisMonth:(NSDate *)date{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    [calendar setFirstWeekday:1];
    NSDateComponents *comp = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:date];
    [comp setDay:1];
    NSDate *firstDayOfMonthDate = [calendar dateFromComponents:comp];
    NSUInteger firstWeekday = [calendar ordinalityOfUnit:NSCalendarUnitWeekday inUnit:NSCalendarUnitWeekOfMonth forDate:firstDayOfMonthDate];
    return firstWeekday - 1;
}

+ (NSInteger)totaldaysInMonth:(NSDate *)date{
    NSRange daysInOfMonth = [[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date];
    return daysInOfMonth.length;
}

+ (NSDate *)lastMonth:(NSDate *)date{
    return [self designatedMonth:date count:-1];
}

+ (NSDate*)nextMonth:(NSDate *)date{
    return [self designatedMonth:date count:+1];
}

+ (NSDate *)designatedMonth:(NSDate *)date count:(NSInteger)count{
    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
    dateComponents.month = count;
    NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:date options:0];
    return newDate;
}

+ (NSDate *)designatedDay:(NSDate *)date count:(NSInteger)count{
    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
    dateComponents.day = count;
    NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:date options:0];
    return newDate;
}

+ (NSDate *)creatDateWithYear:(NSInteger)year month:(NSInteger)month day:(NSInteger)day{
    NSString *timeString = [NSString stringWithFormat:@"%ld年%ld月%ld日",year,month,day];
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    format.dateFormat = @"yyyy年MM月dd日";
    return [format dateFromString:timeString];
}
+ (NSDate *)lastDay:(NSDate *)date{
    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
    dateComponents.day = -1;
    NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:date options:0];
    return newDate;
}
+ (NSString *)currentDayString{
    NSDate *date = [NSDate date];
    NSString *timeString = [NSString stringWithFormat:@"%ld%ld%ld",[NSDate year:date],[NSDate month:date],[NSDate day:date]];
    return timeString;
}
@end
